﻿package cn.com.truelore.service;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import cn.com.truelore.common.Constants;


public class GfoWebService {

	private static HttpTransportSE _HttpTransportSE = null;

	/**
	 * 获取WebService的信息
	 * 
	 * @param 方法名称
	 * @return
	 */
	public static SoapObject getWebServiceInfo(String method) throws Exception {
		SoapObject result = null;
		SoapObject rpc = new SoapObject(Constants.NAMESPACE, method);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);
		envelope.bodyOut = rpc;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(rpc);

		_HttpTransportSE = new HttpTransportSE(Constants.WEBSERVICE_URL);
		_HttpTransportSE.call(Constants.NAMESPACE + method, envelope);

		Object objResponse = envelope.getResponse();
		if (objResponse != null) {
			result = (SoapObject) objResponse;
		}
		return result;
	}

	/**
	 * 获取WebService的信息
	 * 
	 * @param 方法名称
	 * @return
	 */
	public static SoapObject getWebServiceInfo(String method, String[] params,
			String[] values) throws Exception {
		SoapObject result = null;
		// 调用方法
		/*
		 * SoapObject rpc = new SoapObject(Constants.NAMESPACE,
		 * Constants.OP_REGIONLIST);
		 */
		SoapObject rpc = new SoapObject(Constants.NAMESPACE, method);
		if (params != null && values != null && params.length > 0
				&& params.length == values.length)

		{
			for (int i = 0; i < params.length; i++) {
				rpc.addProperty(params[i], values[i]);
			}
		}

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);
		envelope.bodyOut = rpc;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(rpc);

		_HttpTransportSE = new HttpTransportSE(Constants.WEBSERVICE_URL);
		_HttpTransportSE.call(Constants.NAMESPACE + method, envelope);
		// ht.call(null, envelope);

		// SoapObject result = (SoapObject)envelope.bodyIn;
		// detail = (SoapObject)
		// result.getProperty("getWeatherbyCityNameResult");
		Object objResponse = envelope.getResponse();
		if (objResponse != null) {
			result = (SoapObject) objResponse;
		}

		return result;
	}

	/**
	 * 
	 */
	public static void doDispose() {
		try {
			if (_HttpTransportSE != null) {
				_HttpTransportSE.getConnection().disconnect();
				_HttpTransportSE = null;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
